package com.evan.review.fragment;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.evan.review.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

/**
 * ViewPager中的Fragment生命周期演示
 * 展示Fragment在ViewPager中的创建、销毁和恢复过程
 */
public class ViewPagerActivity extends AppCompatActivity {

    private static final String TAG = "ViewPagerActivity";
    private TextView tvLifecycleLog;
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private static final int PAGE_COUNT = 5;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        
        initViews();
        setupViewPager();
    }
    
    private void initViews() {
        tvLifecycleLog = findViewById(R.id.tv_lifecycle_log);
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tab_layout);
    }
    
    private void setupViewPager() {
        // 设置ViewPager适配器
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), getLifecycle());
        viewPager.setAdapter(adapter);
        
        // 设置预加载页面数量为1（默认值）
        viewPager.setOffscreenPageLimit(1);
        
        // 关联TabLayout和ViewPager
        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("Tab " + (position + 1));
            }
        }).attach();
        
        // 添加页面切换监听器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                appendLog("切换到第 " + (position + 1) + " 页");
            }
        });
    }
    
    /**
     * ViewPager适配器
     */
    private class ViewPagerAdapter extends FragmentStateAdapter {
        
        public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }
        
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // 为每个页面创建不同颜色的Fragment
            ColorFragment fragment = ColorFragment.newInstance(
                    "Page " + (position + 1),
                    getPageColor(position),
                    position + 1);
            
            // 设置Fragment回调
            fragment.setCallback(new ColorFragment.FragmentCallback() {
                @Override
                public void onFragmentEvent(String event) {
                    appendLog(event);
                }
            });
            
            return fragment;
        }
        
        @Override
        public int getItemCount() {
            return PAGE_COUNT;
        }
    }
    
    /**
     * 根据位置获取页面颜色
     */
    private int getPageColor(int position) {
        int[] colors = {
                0xFFE57373, // 红色
                0xFF64B5F6, // 蓝色
                0xFF81C784, // 绿色
                0xFFFFD54F, // 黄色
                0xFFBA68C8  // 紫色
        };
        return colors[position % colors.length];
    }
    
    /**
     * 添加日志
     */
    private void appendLog(String log) {
        String currentLog = tvLifecycleLog.getText().toString();
        tvLifecycleLog.setText((currentLog.isEmpty() ? "" : currentLog + "\n") + log);
    }
} 